package com.support.android.myapp3.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.rengwuxian.materialedittext.MaterialEditText;
import com.support.android.myapp3.FragmentEnum;
import com.support.android.myapp3.R;

import java.math.BigDecimal;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.OnTextChanged;
import butterknife.Unbinder;

public class TaxFragment extends Fragment {
    @BindView(R.id.edit_money)
    MaterialEditText moneyEdit;
    @BindView(com.support.android.myapp3.R.id.btn_tax)
    Button taxBtn;
    @BindView(R.id.last_show)
    LinearLayout lastShowLayout;
    @BindView(R.id.edit_sb)
    MaterialEditText editSb;
    private Unbinder unbinder;
    private View view;
    FragmentManager fm;
//    TextInputLayout moneyWrapper;

    private static TaxFragment fa;

    public static TaxFragment getInstance() {
        if (fa == null) {
            fa = new TaxFragment();
        }
        return fa;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_tax, container, false);
        unbinder = ButterKnife.bind(this, view);

        fm = getChildFragmentManager();
//        layout = view.findViewById(R.id.last_show);
//        Button taxBtn = (Button) view.findViewById(R.id.btn_tax);
//        moneyEdit = (MaterialEditText) view.findViewById(R.id.edit_money);
//        moneyWrapper = (TextInputLayout) view.findViewById(R.id.edit_money);

//        taxBtn.setOnClickListener(new MyonclickListener());
//        moneyEdit.addTextChangedListener(new myTextChangeWatcher());

        return view;
    }

    @OnTextChanged(R.id.edit_money)
    public void TextChanged(){
        if(ButterKnife.findById(lastShowLayout,R.id.textlast_show1) == null){
            return ;
        }

        TextView textView2 = (TextView) lastShowLayout.findViewById(R.id.textlast_show1);
//            layout.removeView(textView2);
//            layout.removeViewInLayout(textView2);

        LinearLayout ll = (LinearLayout) view.findViewById(R.id.textlast_show_layout);
        lastShowLayout.removeView(ll);
//            layout.removeViewAt(layout.getChildCount()-1);

        Fragment taxCardFrm = fm.findFragmentByTag(FragmentEnum.TAX_CARD.getName());

        if (taxCardFrm == null) {
            return;
        }
        FragmentTransaction transaction = fm.beginTransaction();

        transaction.remove(taxCardFrm).commit();
    }

    @OnClick(R.id.btn_tax)
    public void btnOnClick(){
        try {
            String moneyStr = moneyEdit.getText().toString();
//                String moneyStr = moneyWrapper.getEditText().getText().toString();
            //判断有无输入
            if (TextUtils.isEmpty(moneyStr) || validateNum(moneyStr) == false) {
//                    moneyWrapper.setError("请输入有效数据");
                moneyEdit.setError("请输入有效数据");
                Toast.makeText(getActivity(), "没有数据输入", Toast.LENGTH_LONG).show();
                return;
            }

            showFragment(FragmentEnum.TAX_CARD, moneyStr);
//                LinearLayout layout = view.findViewById(R.id.last_show);
//                EditText textView = new EditText(getActivity());
//                textView.setId(R.id.text_view_1);
//                textView.setText("2222222222222");


            if (lastShowLayout.getChildCount() == 1) {
                LinearLayout linearLayout1 = (LinearLayout) LayoutInflater.from(getActivity()).inflate(R.layout.fragment_tax_lastshow, lastShowLayout, true);
            }

//                NumberRunningTextView textView2 = (NumberRunningTextView)layout.findViewById(R.id.textlast_show2);
//                textView2.setContent(moneyStr);

            TextView textView1 = (TextView) lastShowLayout.findViewById(R.id.textlast_show1);
            textView1.setText(moneyStr);
//                linearLayout1.addView(textView2);
//                layout.removeView(linearLayout1);
//                layout.add
//                layout.addView(linearLayout1);


//                TextView textView2;

//                if(layout.getChildCount()==1){
//                    textView2 = (TextView) LayoutInflater.from(getActivity()).inflate(R.layout.fragment_tax_lastshow, layout, false);
////                TextView textView2 = (TextView)linearLayout1.findViewById(R.id.textlast_show);
//                    textView2.setText(moneyStr);
//                    layout.addView(textView2);
//                }else{
//                    textView2 = (TextView)layout.findViewById(R.id.textlast_show);
//                    textView2.setText(moneyStr);
//                }
            Toast.makeText(getActivity(), lastShowLayout.getChildCount() + "", Toast.LENGTH_LONG).show();
//                layout.removeViewAt(layout.getChildCount());
//                layout.addView(textView2,1);
        } catch (Exception ex) {
            ex.printStackTrace();
            Toast.makeText(getActivity(), "系统处理异常", Toast.LENGTH_LONG).show();
        }
    }

    private class myTextChangeWatcher implements TextWatcher {

        private CharSequence oldText;

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {


        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
//            if (lastShowLayout.findViewById(R.id.textlast_show1) == null) {
//                return;
//            }

            if(ButterKnife.findById(lastShowLayout,R.id.textlast_show1) == null){
                return ;
            }

            TextView textView2 = (TextView) lastShowLayout.findViewById(R.id.textlast_show1);
//            layout.removeView(textView2);
//            layout.removeViewInLayout(textView2);

            LinearLayout ll = (LinearLayout) view.findViewById(R.id.textlast_show_layout);
            lastShowLayout.removeView(ll);
//            layout.removeViewAt(layout.getChildCount()-1);

            Fragment taxCardFrm = fm.findFragmentByTag(FragmentEnum.TAX_CARD.getName());

            if (taxCardFrm == null) {
                return;
            }
            FragmentTransaction transaction = fm.beginTransaction();

            transaction.remove(taxCardFrm).commit();
        }
    }

    public boolean validateNum(String num) {
        try {
            new BigDecimal(num);
        } catch (Exception ex) {
            return false;
        }

        return true;
    }

    private class MyonclickListener implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            try {
                String moneyStr = moneyEdit.getText().toString();
//                String moneyStr = moneyWrapper.getEditText().getText().toString();
                //判断有无输入
                if (TextUtils.isEmpty(moneyStr) || validateNum(moneyStr) == false) {
//                    moneyWrapper.setError("请输入有效数据");
                    moneyEdit.setError("请输入有效数据");
                    Toast.makeText(getActivity(), "没有数据输入", Toast.LENGTH_LONG).show();
                    return;
                }

                showFragment(FragmentEnum.TAX_CARD, moneyStr);
//                LinearLayout layout = view.findViewById(R.id.last_show);
//                EditText textView = new EditText(getActivity());
//                textView.setId(R.id.text_view_1);
//                textView.setText("2222222222222");


                if (lastShowLayout.getChildCount() == 1) {
                    LinearLayout linearLayout1 = (LinearLayout) LayoutInflater.from(getActivity()).inflate(R.layout.fragment_tax_lastshow, lastShowLayout, true);
                }

//                NumberRunningTextView textView2 = (NumberRunningTextView)layout.findViewById(R.id.textlast_show2);
//                textView2.setContent(moneyStr);

                TextView textView1 = (TextView) lastShowLayout.findViewById(R.id.textlast_show1);
                textView1.setText(moneyStr);
//                linearLayout1.addView(textView2);
//                layout.removeView(linearLayout1);
//                layout.add
//                layout.addView(linearLayout1);


//                TextView textView2;

//                if(layout.getChildCount()==1){
//                    textView2 = (TextView) LayoutInflater.from(getActivity()).inflate(R.layout.fragment_tax_lastshow, layout, false);
////                TextView textView2 = (TextView)linearLayout1.findViewById(R.id.textlast_show);
//                    textView2.setText(moneyStr);
//                    layout.addView(textView2);
//                }else{
//                    textView2 = (TextView)layout.findViewById(R.id.textlast_show);
//                    textView2.setText(moneyStr);
//                }
                Toast.makeText(getActivity(), lastShowLayout.getChildCount() + "", Toast.LENGTH_LONG).show();
//                layout.removeViewAt(layout.getChildCount());
//                layout.addView(textView2,1);
            } catch (Exception ex) {
                ex.printStackTrace();
                Toast.makeText(getActivity(), "系统处理异常", Toast.LENGTH_LONG).show();
            }

        }
    }

    /**
     * onDestroyView中进行解绑操作
     */
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    public void showFragment(FragmentEnum tag, String money) {
        Fragment to = fm.findFragmentByTag(tag.getName());
//
//
        to = new TaxCardFragment();
        Bundle bundle = new Bundle();
        bundle.putString("money", money);
        to.setArguments(bundle);

        FragmentTransaction transaction = fm.beginTransaction();

        transaction.replace(R.id.fl_tax, to, tag.getName()).commit();

//        Toast.makeText(getActivity(), "showFragment", Toast.LENGTH_LONG).show();
    }

}
